#include <Arduino.h>
#include <Servo.h>
#include <Wire.h> //Arduino内置IIC
#include "remote.h"
#include "screen.h"
#include "wifi.h"
#include "server.h"
#include "state.h"
#include "serial.h"

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>BUTTON
#define BUTTON_PIN 15 //接了外部下拉
#define BUTTON_PIN_2 2 //有外部上拉
bool Button_Pressed = false;
const unsigned long DebounceDelay = 50; //两次按下不能短于 50ms
bool Button_Pressed_2 = false;
unsigned long LastPressTime = 0; // 记录上次按下的时间
unsigned long LastPressTime_2 = 0;


bool DataUpdated = false;

void IRAM_ATTR HandleButtonPress() { //ICACHE_RAM_ATTR函数强制存到RAM里
  if (millis() - LastPressTime < DebounceDelay) { // 忽略抖动
    return;
  }
  Button_Pressed = true;
}
void IRAM_ATTR HandleButtonPress2() {
  if (millis() - LastPressTime_2 < DebounceDelay) { // 忽略抖动
    return;
  }
  Button_Pressed_2 = true;
}


void setup() {
  Serial.begin(115200);
  ScreenInit();

  pinMode(BUTTON_PIN, INPUT); 
  attachInterrupt(BUTTON_PIN, HandleButtonPress, RISING);
  pinMode(BUTTON_PIN_2, INPUT_PULLUP);
  attachInterrupt(BUTTON_PIN_2, HandleButtonPress2, FALLING);
  ShowNotConnected();
  Serial.print("Connetcting to WiFi...");
  ConnectWifi();
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Wifi Connected.");
    ShowNotConnected(1);
    Serial.print("Locacl Ip: ");
    Serial.println(WiFi.localIP());  // 打印本机IP
    while(!ConnectRemote(getRemoteIP())) {
      ShowNotConnected(1);
    }
    Serial.println("Remote Connected.");
    Moe();
  }
}

int b1=0,b2=0;
bool screenUpdate = false;
void loop() {
  
  String inString="";
  while(Serial.available()>0){
    inString += char(Serial.read());
    delay(10);      // 延时函数用于等待字符完全进入缓冲区
  }
  HandleSerialMessage(inString);
    // 按键发送
  if (Button_Pressed && client.connected()) {
    b1++;
    screenUpdate = true;
    Button_Pressed = false;
  }
  if (Button_Pressed_2 && client.connected()) {
    b2++;
    screenUpdate = true;
    Button_Pressed_2 = false;
  }
  
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Wifi Disconnected.");
    ShowNotConnected();
    TryReconnectWifi();
    if(WiFi.status() == WL_CONNECTED) {
      Serial.println("Wifi Connected.");
      ShowNotConnected(1);
      while(!ConnectRemote(getRemoteIP())) {
      }
      Serial.println("Remote Connected.");
      Moe();
    }
    return;
  }
  checkConnection();
  if(RemoteDataUpdated()) { //此函数内部会接收tcp消息，判断并解析包
    screenUpdate = true;
  }
  if(screenUpdate) {
    char buffer[32];
    //更新屏幕
    int x = (int)dataItems.x;
    int y = (int)dataItems.y;
    int z = (int)dataItems.z;
    int yaw = (int)dataItems.yaw;
    int thr = dataItems.Throttle;
    int bat = (int)dataItems.battery;
    u8g2.clearBuffer();
    sprintf(buffer, "xyz:%+4d%+4d%+4d", x, y, z);
    u8g2.drawStr(0,10,buffer);
    sprintf(buffer, "bty:%3d %3d %3d", bat*10, thr/10, yaw);
    u8g2.drawStr(0,27,buffer);
    //第三行的y为43
    sprintf(buffer, "b1: %3d  b2: %3d", b1,b2);
    u8g2.drawStr(0,60,buffer);
    u8g2.sendBuffer();
    screenUpdate = false;
  }
}